package com.ciban.smarthome.module.ui.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import com.ciban.smarthome.R;
import com.ciban.smarthome.module.entity.EquiTypeInfo;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AreaEquListAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<EquiTypeInfo> equipmentList;
	private AssetManager assetManager;

	public AreaEquListAdapter(Context context, ArrayList<EquiTypeInfo> list) {
		this.context = context;
		this.equipmentList=list;
	}

	@Override
	public int getCount() {
		return equipmentList.size();
	}

	@Override
	public Object getItem(int position) {
		return equipmentList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(context,
					R.layout.griditem_common_content, null);
			holder = new ViewHolder();
			holder.iv_icon = (ImageView) convertView
					.findViewById(R.id.item_gv_content_iv_icon);
			holder.tv_desc = (TextView) convertView
					.findViewById(R.id.item_gv_content_tv_title);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		EquiTypeInfo info = equipmentList.get(position);

		holder.tv_desc.setText(info.getName());

		if (assetManager == null)
			assetManager = context.getAssets();
		InputStream in = null;
		Bitmap bmp = null;
		try {
			in = assetManager.open(info.getIcon_path());
			bmp = BitmapFactory.decodeStream(in);
			holder.iv_icon.setImageBitmap(bmp);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return convertView;
	}

	static class ViewHolder {
		ImageView iv_icon;
		TextView tv_desc;
	}

}
